require 'RMagick'

# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  # Pick a unique cookie name to distinguish our session data from others'
  session :session_key => '_webalbum_session_id' 
  
  def authorize 
    unless session[:user_id]
      info("Please log in", :error)
      redirect_to( :controller => "/login" , :action => "login" )
    end
  end
  
  def info(message, type = :error) 
    flash[type] = message
  end
  
  def validate_current_catalog
    
    
    catalog_id = session[:current_catalog_id]
    @current_catalog = @current_catalog = (catalog_id.nil?) ? nil : (Catalog.find(catalog_id))
    @user_id = (session[:user_id].nil?) ? -1 : session[:user_id]    

    if @current_catalog.nil?
      info "You have taken the wrong way, my friend!"
      redirect_to(:controller => "/user/catalog", :action => "index" )
      return false
    end
      
    if !@current_catalog or !@current_catalog.is_accessible?(@user_id)
      info "Invalid or restricted directory"
      redirect_to(:controller => "/user/catalog", :action => "index" )
      return false
    end
        
  end

  def validate_ownership
        
    catalog_id = session[:current_catalog_id]
    @current_catalog = (catalog_id.nil?) ? nil : (Catalog.find(catalog_id))
    @user_id = (session[:user_id].nil?) ? -1 : session[:user_id]    
               
    if @current_catalog.nil?
      info "You have taken the wrong way, my friend!"
      redirect_to(:controller => "/user/catalog", :action => "index" )
      return false
    end
    
    if @current_catalog.owner.id != @user_id        
      info "Cannot edit directory that you don't own"
      redirect_to(:controller => "/user/catalog", :action => "index" )
      return false
    end
    
  end
  
  def get_picture
    @picture = Picture.find( params[:id] )    
    pict = Magick::Image.read(@picture.get_path()).first        
    
    if(params[:view].nil? || params[:view] == 0) # Original view.
      
    elsif (params[:view] == 1) # Sample view.
      pict.sample!(320, 240)
    elsif (params[:view] == 2) # Thumbnail view.
      pict.thumbnail!(10, 10)    
    end
        
    send_data(pict.to_blob, :type=> @picture.content_type, :disposition=>'inline')
  end
  
  def get_folder_picture
    pict = Magick::Image.read("#{RAILS_ROOT}/images/Vista-package_wordprocessing.png").first
    pict.thumbnail!(100, 100)
    send_data(pict.to_blob, :type=> "image/png", :disposition=>'inline')
    #if params[:id] and (@picture = Picture.find(params[:id]))
    #  image_array = Magick::ImageList.new("#{RAILS_ROOT}/images/Vista-package_wordprocessing.png", @picture.get_path())     
    #  image_array[0].thumbnail!(100, 100)      
    #  image_array[1].thumbnail!(10, 10)
      #image_array[1].page = Rectangle.new(70, 70, 20, 20)
    #else
    #  puts "IOOODAA"
    #  image_array = Magick::ImageList.new("#{RAILS_ROOT}/images/Vista-package_wordprocessing.png")     
    #  image_array[0].thumbnail!(100, 100)    
    #end
    #pict = image_array.flatten
    #send_data(pict.to_blob, :type => "image/png", :disposition => 'inline')
  end
end
